package com.example.pinkcat.adapter;

import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.fragment.app.FragmentActivity;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.manager.ConnectivityMonitor;
import com.example.pinkcat.R;
import com.example.pinkcat.activity.VideoActivity;
import com.example.pinkcat.activity.VideoPinkcat;
import com.example.pinkcat.activity.ViewAllMyFollow;
import com.example.pinkcat.activity.api.entity.VideoEntity;
import com.example.pinkcat.api.entity.VideowaibuEntity;
import com.example.pinkcat.api.entity.videoMsg;
import com.example.pinkcat.api.getData.getData_waibu;
import com.example.pinkcat.fragment.shouye.Fragment_tuijian;
import com.rishabhharit.roundedimageview.RoundedImageView;
import com.youth.banner.Banner;
import com.youth.banner.BannerConfig;
import com.youth.banner.Transformer;
import com.youth.banner.listener.OnBannerListener;
import com.youth.banner.loader.ImageLoader;

import java.util.ArrayList;
import java.util.List;

public class GallerView_RecyclerViewAdapter extends RecyclerView.Adapter<GallerView_RecyclerViewAdapter.ViewHolder> {
    private final Context context;
    private Fragment_tuijian fragment_tuijian;


    View viewbar;
    int count = 0;

    List<VideowaibuEntity> videoMsgArrayList=new ArrayList<>();
    List<VideowaibuEntity> videobannerEnt=new ArrayList<>();
    List<VideowaibuEntity> video_new=new ArrayList<>();



    public void addBannerData(List<VideowaibuEntity> videoEntities){
        this.videobannerEnt.addAll(videoEntities);
        notifyDataSetChanged();
    }

    public void addData(List<VideowaibuEntity> videoEntities){
        this.videoMsgArrayList= videoEntities;
        notifyDataSetChanged();
    }

    public void addData_new(List<VideowaibuEntity> videoEntities){
        this.videoMsgArrayList.addAll(videoEntities);
        notifyDataSetChanged();
    }

    public List<VideowaibuEntity> getData(){
        return this.video_new;
    }



    public GallerView_RecyclerViewAdapter(Context context,Fragment_tuijian fragment_tuijian) {
        this.context = context;
        this.fragment_tuijian=fragment_tuijian;
    }

    static class ViewHolder extends RecyclerView.ViewHolder{
         RoundedImageView imageView_nomorl;
         TextView textViewDescription_nomorl;
         TextView textViewTitlte_nomorl;
         View video;
         TextView list_video_tx_pf;
         TextView list_video_tx_pl;
         Banner mBanner;
        public ViewHolder(@NonNull View itemView) {
            super(itemView);

            imageView_nomorl=itemView.findViewById(R.id.lis_manga_img_nomorl);
            textViewTitlte_nomorl=itemView.findViewById(R.id.manga_title_nomorl);
            textViewDescription_nomorl=itemView.findViewById(R.id.manga_descrptioon_nomorl);
            list_video_tx_pf = itemView.findViewById(R.id.list_video_tx_pf);
            list_video_tx_pl = itemView.findViewById(R.id.list_video_tx_pl);
            mBanner = itemView.findViewById(R.id.mBanner);           //Banner
            video = itemView.findViewById(R.id.video_normal);

        }
    }

    //创建view
    @NonNull
    @Override
    public GallerView_RecyclerViewAdapter.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view;
        if (viewType==0){
            view= LayoutInflater.from(context).inflate(R.layout.layout_banner,parent,false);//轮播图
        }
        else {
            view= LayoutInflater.from(context).inflate(R.layout.layout_manga,parent,false);//正常视图
        }

        return new GallerView_RecyclerViewAdapter.ViewHolder(view);
    }

    //数据绑定
    @Override
    public void onBindViewHolder(@NonNull GallerView_RecyclerViewAdapter.ViewHolder holder, int position) {

        if (position==getItemCount()-1&&position<31){
            System.out.println(videoMsgArrayList);
            getData_waibu.getVideo_new(context,GallerView_RecyclerViewAdapter.this);
        }

        //页面跳转
        if (position==0){
            initViews(holder,position);
        }else {

            holder.textViewTitlte_nomorl.setText(videoMsgArrayList.get(position-1).getVideo_name()); //视频标题
            holder.textViewDescription_nomorl.setText(videoMsgArrayList.get(position-1).getVideo_info()); //视频描述
            Glide.with(context).load(Uri.parse(videoMsgArrayList.get(position-1).getVideo_cover())).into(holder.imageView_nomorl); // 视频封面
            holder.list_video_tx_pf.setText(String.valueOf(videoMsgArrayList.get(position-1).getTotalplay())); //播放量
            holder.list_video_tx_pl.setText(String.valueOf(videoMsgArrayList.get(position-1).getComment_count())); //评论量

            holder.video.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (videoMsgArrayList.get(position-1).getVideo_type().equals("综艺")||
                            videoMsgArrayList.get(position-1).getVideo_type().equals("电视剧")||
                            videoMsgArrayList.get(position-1).getVideo_type().equals("电影")||
                            videoMsgArrayList.get(position-1).getVideo_type().equals("动漫"))
                    {
                        Intent intent=new Intent(context, VideoPinkcat.class);
                        intent.putExtra("v_id",videoMsgArrayList.get(position-1).getVideo_id());
                        intent.putExtra("up_id",videoMsgArrayList.get(position-1).getUser_id());
                        intent.putExtra("v_link",videoMsgArrayList.get(position-1).getVideo_link());
                        intent.putExtra("v_labs",videoMsgArrayList.get(position-1).getVideo_lab());
                        context.startActivity(intent);
                    }
                    else {
                        Intent intent=new Intent(context, VideoActivity.class);
                        intent.putExtra("v_id",videoMsgArrayList.get(position-1).getVideo_id());
                        intent.putExtra("up_id",videoMsgArrayList.get(position-1).getUser_id());
                        intent.putExtra("v_link",videoMsgArrayList.get(position-1).getVideo_link());
                        intent.putExtra("v_labs",videoMsgArrayList.get(position-1).getVideo_lab());
                        context.startActivity(intent);
                    }
                }
            });
        }

    }

    @Override
    public int getItemCount() {
        return videobannerEnt.size()==0? videoMsgArrayList.size():videoMsgArrayList.size()+1;
    }

    //返回数据类型
    @Override
    public int getItemViewType(int position) {
        if (position==0){
            return 0;
        }
        else {
            return 1;
        }
    }
    private void initViews(ViewHolder viewHolder,int position) {


        //图片资源
        // int[] imageResourceID = new int[]{R.drawable.sucai1,R.drawable.sucai2,R.drawable.sucai3};
        //List<Integer> imageList = new ArrayList<>();
        List<Uri> imageyriList = new ArrayList<>();
        //轮播标题
        List<String> titileList = new ArrayList<>();
        if (videobannerEnt.size()<3)
            return;
        for(int i=0; i< 3; i++){
            imageyriList.add(Uri.parse(videobannerEnt.get(position+i).getVideo_cover()));
            titileList.add(videobannerEnt.get(position+i).getVideo_name());//把标题循环设置进列表里
        }
        viewHolder.mBanner.setImageLoader(new ImageLoader() {
            @Override
            public void displayImage(Context context, Object path, ImageView imageView) {
                Glide.with(fragment_tuijian).load(path).fitCenter().into(imageView);
            }
        });
        //设置轮播的动画效果，可以去github上查看文档
        viewHolder.mBanner.setBannerAnimation(Transformer.Accordion);
        //设置图片资源
        viewHolder.mBanner.setImages(imageyriList);
        //设置自动轮播，默认为true
        viewHolder.mBanner.isAutoPlay(true);
        //设置样式,默认为:Banner.NOT_INDICATOR(不显示指示器和标题)
        //可选样式如下:
        //1. Banner.CIRCLE_INDICATOR    显示圆形指示器
        //2. Banner.NUM_INDICATOR   显示数字指示器
        //3. Banner.NUM_INDICATOR_TITLE 显示数字指示器和标题
        //4. Banner.CIRCLE_INDICATOR_TITLE  显示圆形指示器和标题
        //设置图片加载器,通过Glide加载图片
        //设置banner显示的样式（带标题的样式）
        viewHolder.mBanner.setBannerStyle(BannerConfig.CIRCLE_INDICATOR_TITLE_INSIDE);

        //设置标题集合（当banner样式有显示title时）
        viewHolder.mBanner.setBannerTitles(titileList);
        //设置是否允许手动滑动轮播图
        viewHolder.mBanner.setViewPagerIsScroll(true);
        //设置指示器位置(即图片下面的那个小圆点)
        //设置轮播样式（没有标题默认为右边,有标题时默认左边）
        //可选样式:
        //Banner.LEFT   指示器居左
        //Banner.CENTER 指示器居中
        //Banner.RIGHT  指示器居右
        viewHolder.mBanner.setIndicatorGravity(BannerConfig.CENTER);
        //设置轮播时间3秒切换下一图
        viewHolder.mBanner.setDelayTime(3000);
        viewHolder.mBanner.setImages(imageyriList).setOnBannerListener(new OnBannerListener() {
            @Override
            public void OnBannerClick(int position) {
                if (position == 0){
                    Intent intent = new Intent(context,VideoActivity.class);
                    intent.putExtra("v_id",videoMsgArrayList.get(position).getVideo_id());
                    intent.putExtra("up_id",videoMsgArrayList.get(position).getUser_id());
                    intent.putExtra("v_link",videoMsgArrayList.get(position).getVideo_link());
                    intent.putExtra("v_labs",videoMsgArrayList.get(position).getVideo_lab());
                    context.startActivity(intent);
                }else if (position ==1){
                    Intent intent = new Intent(context,VideoActivity.class);
                    intent.putExtra("v_id",videoMsgArrayList.get(position).getVideo_id());
                    intent.putExtra("up_id",videoMsgArrayList.get(position).getUser_id());
                    intent.putExtra("v_link",videoMsgArrayList.get(position).getVideo_link());
                    intent.putExtra("v_labs",videoMsgArrayList.get(position).getVideo_lab());
                    context.startActivity(intent);
                }else {
                    Intent intent = new Intent(context,VideoActivity.class);
                    intent.putExtra("v_id",videoMsgArrayList.get(position).getVideo_id());
                    intent.putExtra("up_id",videoMsgArrayList.get(position).getUser_id());
                    intent.putExtra("v_link",videoMsgArrayList.get(position).getVideo_link());
                    intent.putExtra("v_labs",videoMsgArrayList.get(position).getVideo_lab());
                    context.startActivity(intent);
                }
            }
        }).start();
        viewHolder.mBanner.startAutoPlay();

    }

}